package top.leyi.handle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.leyi.IService.IDataService;
import top.leyi.entity.Talk.SimpleTalk;
import top.leyi.entity.Talk.Talk;
import top.leyi.entity.data.Data;
import top.leyi.entity.data.Type;
import top.leyi.entity.user.User;
import top.leyi.utils.Result;
import top.leyi.utils.ResultGenerator;

import java.util.List;

/**
 * @desd 资料分享
 */

@RequestMapping("/api/data")
@RestController
public class DataHandle {



    @Autowired
    IDataService dataService;


    /**
     * @desc 获取指定范围資料
     * @param offset
     * @return
     */
    @RequestMapping(value = "/type", method = RequestMethod.GET)
    public Result doGet(
                        @RequestParam(value = "token") String token) {

        List<Type> data = dataService.getAllDataType( );
        return new Result(200,data );
    }


    /**
     * @desc 获取指定范围資料
     * @param offset
     * @return
     */
    @RequestMapping(value = "/{offset}", method = RequestMethod.GET)
    public Result doGet(@PathVariable(value = "offset") int offset,
                        @RequestParam(value = "key", required = false, defaultValue = "") String key,
                        @RequestParam(value = "token") String token,
                        @RequestParam(value = "type", required = false, defaultValue = "") String type) {


        List<Data> data  = null;
        if(type.equals("")) {
           data = dataService.getAllData(offset, key);
        }
        else {

            data = dataService.getAllDataWithType(offset,key, type);
        }
        return new Result(200,data );
    }


    /**
     *  @desc 上传資料
     * @param content
     * @param pics
     * @param userId
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST )
    public Result doPost(@RequestParam(value = "name" ) String name,
                        @RequestParam(value = "details" ) String details,
                        @RequestParam(value = "id", required = true) int userId,
                         @RequestParam(value = "url", required = true) String url,
                         @RequestParam(value = "type", required = true) String type,
                        @RequestParam(value = "token", required = true) String token){

        Data data = new Data();
        data.setName(name);
        data.setUserId( userId );
        data.setType(type);
        data.setDetails(details);
        data.setUrl(url);

        User user = new User();
        user.setId(userId);
        user.setToken(token);

        dataService.addData(data, user);

        return ResultGenerator.OK.getResult();
    }



    /**
     *  @desc 刪除資料
     * @param content
     * @param pics
     * @param userId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.DELETE )
    public Result doPost(@RequestParam(value = "dataId" ) int dataId,
                         @RequestParam(value = "id" ) int userId,
                         @RequestParam(value = "token", required = true) String token){

        Data data = new Data();
        data.setId(dataId);

        User user = new User();
        user.setId(userId);
        user.setToken(token);

        dataService.removeData(data, user);

        return ResultGenerator.OK.getResult();
    }


}
